﻿using AppCommerciaux.Entity;
using AppCommerciaux.ViewModels;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace AppCommerciaux.Views
{
    /// <summary>
    /// Logique d'interaction pour AccueilPrincipalView.xaml
    /// </summary>
    public partial class AccueilPrincipalView : Window, INotifyPropertyChanged
    {
        public AccueilPrincipalViewModel _mainViewModel;

        public AccueilPrincipalView(AccueilPrincipalViewModel MainViewModel)
        {
            DataContext = _mainViewModel = MainViewModel;
            DataContext = this; 
            ButtCancel = true;
            InitializeComponent();

        }


        #region Declaration

        private bool _buttCancel;
        public bool ButtCancel
        {
            get { return _buttCancel; }
            set
            {
                _buttCancel = value;
                OnPropertyChanged("ButtCancel");
            }
        }
        
        private UserControl _currentPage;
        public UserControl CurrentPage
        {
            get { return _currentPage; }
            set
            {
                _currentPage = value;
                this.OnPropertyChanged("CurrentPage");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        #endregion

        #region Load & event Pages

        private void buttonDeconnexion_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult result = MessageBox.Show("Voulez-vous vous déconnecter ?", "Déconnexion...", MessageBoxButton.YesNo);

            if (result == MessageBoxResult.Yes)
            {
                // Mise à jour du fichier login.lic et des doonnées de connexion de la classe Authentification
                Authentification.Deconnexion();

                // On désactive les boutons du menu
                buttonClient.IsEnabled = false;
                buttonCommande.IsEnabled = false;
                buttonDeconnexion.IsEnabled = false;
                buttonRendezVous.IsEnabled = false;
                buttonSynchronisation.IsEnabled = false;
                buttonChangerMdp.IsEnabled = false;

                // On vide l'écran
                centralGrid.Children.Clear();

                // On Charge et affiche l'écran de connexion
                UserControlAuthentification ucAuthentification = new UserControlAuthentification(this);
                centralGrid.Children.Add(ucAuthentification);
            }


        }
        private void buttonChangerMdp_Click(object sender, RoutedEventArgs e)
        {

            // On vide l'écran
            centralGrid.Children.Clear();

            // On Charge et affiche l'écran de connexion
            UserControlAuthentification ucAuthentification = new UserControlAuthentification("CMdP");
            centralGrid.Children.Add(ucAuthentification);
        }
        private void ButtonRDV_Click(object sender, RoutedEventArgs e)
        {
            LoadRDVPage();
        }
        private void ButtonClient_Click(object sender, RoutedEventArgs e)
        {
            LoadClientPage();
        }     
        private void buttonSynchronisation_Click(object sender, RoutedEventArgs e)
        {
            SynchroViewModel svm = new SynchroViewModel();
            UserControlSynchro windowSynchro = new UserControlSynchro(svm, this);
            centralGrid.Children.Clear();
            centralGrid.Children.Add(windowSynchro);
            

        }
        private void Window_Closing_1(object sender, CancelEventArgs e)
        {
            MessageBoxResult result = MessageBox.Show("Voulez-vous quitter l'application ?", "Quitter...", MessageBoxButton.YesNo);
            if (result == MessageBoxResult.No)
            {
                // Annuler la fermeture de la fenêtre
                e.Cancel = true;
            }
            else
            {
                // Déconnexion et poursuite de l'évènement de fermeture
                Authentification.Deconnexion();
            }
        }
        private void LoadRDVPage()
        {
            ConsulterRendezVousView windowSynchro = new ConsulterRendezVousView();
            centralGrid.Children.Clear();
            centralGrid.Children.Add(windowSynchro);

            //ButtCancel = true;
            //OnPropertyChanged("ButtCancel");
        }
        private void LoadClientPage()
        {
            ConsulterClientView windowSynchro = new ConsulterClientView();
            centralGrid.Children.Clear();
            centralGrid.Children.Add(windowSynchro);

            //ButtCancel = true;
            //OnPropertyChanged("ButtCancel");
        }

        #endregion

        #region Load LigneCommandeView/LigneCommandeEditView
        public void LoadLigneCommandeEditPage(string Etat, commandeclient SelectedCommande)
        {
            LigneCommandeEditViewModel cvm = new LigneCommandeEditViewModel(Etat, SelectedCommande);
            LigneCommandeEditView EditPage = new LigneCommandeEditView(cvm);

            App.Apv.centralGrid.Children.Clear();
            App.Apv.centralGrid.Children.Add(EditPage);
        }

        public void QuitLigneCommandeEditPage()
        {
            App.Apv.centralGrid.Children.Clear();
            App.Apv.centralGrid.Children.Add(CurrentPage);
        }

        #endregion

        #region Load CommandeView/CommandeEditView
        private void ButtonCommande_Click(object sender, RoutedEventArgs e)
        {
            LoadCommandeClientView();
        }
        public void LoadCommandeClientView()
        {
            CommandeViewModel cvm = new CommandeViewModel();
            CurrentPage = new CommandeView(cvm);
            centralGrid.Children.Clear();
            centralGrid.Children.Add(CurrentPage);
        }
        public void LoadEditCommandeClientView(string Etat, commandeclient selectedCommande)
        {
            CommandeEditViewModel cvm = new CommandeEditViewModel(Etat, selectedCommande);
            CurrentPage = new CommandeEditView(cvm);
            App.Apv.centralGrid.Children.Clear();
            App.Apv.centralGrid.Children.Add(CurrentPage);
        }


        #endregion


        protected void OnPropertyChanged(string propertyName)
        {
            this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }
        protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            var handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, e);
            }
        }


    }
}
